#include "dao.h"

int IsExit(char *name,char *pwd)
{
  int result;
  int DBresult;
  sqlite3 *d = NULL;
  const char *dbfile="/home/linyanqun/test.db";
  char *errmsg=NULL;
  char **QueryResult;
  int i,row,col;
  DBresult=sqlite3_open(dbfile,&d);
  if(DBresult!=0)
  {
   printf("can't open DB!");
   return 0;
  }

  char sqlcmd[256]="select * from users where name ='";
  strcat(sqlcmd,name);
  strcat(sqlcmd,"' AND pwd='");
  strcat(sqlcmd,pwd);
  strcat(sqlcmd,"';");
 // puts(sqlcmd);
  int res=sqlite3_get_table(d,sqlcmd,&QueryResult,&row,&col,&errmsg);
  if(res!=SQLITE_OK)
  {
    printf("DB WRONG!");
    DBresult=sqlite3_close(d);
    return 0;
  }
  else if(row<1)
  {

#ifdef _DEBUG
    puts("Fail to Query!");
#endif

    DBresult=sqlite3_close(d);
    return 0;
  }
  else
  {
    sqlite3_free_table(QueryResult);
    DBresult=sqlite3_close(d);
    return 1;
  }
  }   

int Expend(char *name,char *pwd,char *deadtime)
 {
    int result;
    int DBresult;
    sqlite3 *d = NULL;
    const char *dbfile="/home/linyanqun/test.db";
    char *errmsg=NULL;
    DBresult=sqlite3_open(dbfile,&d);
    if (DBresult!=0)
    {
      printf("can't open DB!");
      return 0;
    }
    char sqlcmd[256]="insert into users values('";
    strcat(sqlcmd,name);
    strcat(sqlcmd,"','");
    strcat(sqlcmd,pwd);
    strcat(sqlcmd,"','");
    strcat(sqlcmd,deadtime);
    strcat(sqlcmd,"');");
  //  puts(sqlcmd);
    int res=sqlite3_exec(d,sqlcmd,NULL,NULL,&errmsg);
    if(res!=SQLITE_OK)
  {
    printf("DB WRONG!");
    DBresult=sqlite3_close(d);
    return 0;
  }

  else
  {
    DBresult=sqlite3_close(d);
    return 1;
  }
  }
/* 
int main() 
{
  char *name="lin";
  char *pwd="123456";
  int res=IsExit(name,pwd);
  if(res)
  printf("Query successfully!");
  else
  {
   printf("Fail to Query,haha!");
  }
  char *name_1="liu";
  char *pwd_1="123456";
  char *deadtime_1="2015-09-01";
  int res_1=Expend(name_1,pwd_1,deadtime_1);
  if(res_1)
    printf("Expend successfully!");
  else
  {
   printf("Fail to Expend,haha!");
   return 1;
  }
  return 0;
  }    
*/
